import config from './config.js'
import {getToken, genUrl, delCache, removeToken, getCache} from './fun.js'

let sendSum = 0 // 需要展示 Loading发送请求的数量 为0才关闭
let sendTime

function request(uri, method, data, query, is_login) {
    let local = getCache('lang');
    let header = {
        'X-Requested-With': 'XMLHttpRequest',
        'app-source': config.app_source,
        'accept-language': local,
        'app-version': config.app_version,
        'account-type': config.account_type,
        'channel': config.channel
    }
    if (is_login) {
        header['Authorization'] = 'Bearer ' + getToken()
    }
    let url = genUrl(config.api_url + uri, query)
    return new Promise((resolve, reject) => {
        if (data.loding || query.loding) {
            sendSum += 1;
            uni.showLoading({
                title: 'loding...',
                mask: true
            })
        }
        uni.request({
            url: url,
            method: method,
            header: header,
            data: data,
            timeout: config.api_timeout,
            success: response => {
                let res = response.data;
                let showMsg = false
                let msg = res.message
                if (data.returnAll === 1 || query.returnAll === 1) {
                    return resolve(res)
                }
                if (res.code === 200) {
                    return resolve(res.data)
                } else if (res.code === 5001 || res.code === 1002) {
                    msg = res.message
                    showMsg = true
                } else if (res.code === 1001 || res.code === 400) { //token无效
                    removeToken()
                    delCache('cache')
                    uni.reLaunch({
                        url: '/pages/login/login'
                    })
                }

                if (showMsg) {
                    uni.showToast({
                        title: msg,
                        icon: 'none'
                    })
                }
                sendSum = 0
                return reject(new Error(res.message || 'Error'))
            },
            fail: error => {
                sendSum = 0
                if (data.returnAll === 1) {
                    return resolve(error)
                }
                uni.showToast({
                    title: "网络异常",
                    icon: 'none'
                })
                return reject(error)
            },
            complete() {
                if (data.loding || query.loding) {
                    sendSum -= 1;
                    clearTimeout(sendTime)
                    sendTime = setTimeout(() => {
                        if (sendSum === 0 || sendSum < 0) {
                            uni.hideLoading();
                            sendSum = 0
                        }
                    }, 200)
                }
            }
        })
    })
}

function get(url, query = {}, is_login = true) {
    return request(url, 'GET', {}, query, is_login)
}

function post(url, data = {}, query = {}, is_login = true) {
    return request(url, 'POST', data, {}, is_login)
}

function put(url, data = {}, query = {}, is_login = true) {
    return request(url, 'PUT', data, query, is_login)
}

function del(url, data = {}, query = {}, is_login = true) {
    return request(url, 'DELETE', data, query, is_login)
}

export default {
    get,
    post,
    put,
    del
}